﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models.WeChat.Dtos.MPConfig;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.WeChat
{
    /// <summary>
    /// 公众号配置
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2020.09.01 
    /// </summary>
    [Route("api/WeChat/[controller]")]
    public class QYHConfigController : BaseApiController
    {
        private SysConfigBll sysConfigBll = new SysConfigBll();

        #region Get
        /// <summary>
        /// 获取公众号配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetInfo()
        {
            var data = (await sysConfigBll.GetList()).FindAll(x => x.Category == "QYHConfig");
            var dic = new Dictionary<string, string>();
            foreach (var item in data)
            {
                dic.Add(item.Key, item.Value);
            }
            return Success(dic);
        }
        #endregion

        #region Post
        /// <summary>
        /// 更新公众号配置信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResponseResult> Update([FromBody] QYHConfigUpInput input)
        {
            var data = (await sysConfigBll.GetList()).FindAll(x => x.Category == "QYHConfig");
            var dic = input.MapTo<Dictionary<string, string>>();
            foreach (var item in data)
            {
                if (dic.ContainsKey(item.Key))
                {
                    item.Value = dic[item.Key].ToString();
                }
            }
            await sysConfigBll.Update(data);
            return Success("修改成功");
        }
        #endregion
    }
}
